using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.IO;
using System.Net.Mail;
using System.Text;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
using TanashCMS.Business.Utilities;
using System.Net;
using log4net;

public partial class contact : CMSPageBase
{
    ILog log = log4net.LogManager.GetLogger("default");
    private bool messageSent = false;

    protected override void OnInit(EventArgs e)
    {
        if (Request.Form.Count > 0)
            PageGuid = "sent";
        base.OnInit(e);
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.Form.Count > 0)
        {
            string senderName = Request.Form["yourName"];
            string senderEmail = Request.Form["yourEmail"];
            string message = Request.Form["message"];

            try
            {
                log.DebugFormat("contact us email from {0}, to {1}", senderName, senderEmail);
                Trace.Write(string.Format("contact us email from {0}, to {1}", senderName, senderEmail));
                //create the mail message
                MailMessage mail = new MailMessage();

                //set the addresses
                log.DebugFormat("mail.From = {0}", senderName);
                mail.From = new MailAddress(senderEmail, senderName);
                mail.ReplyTo = new MailAddress(senderEmail, senderName);

                // TODO: this should not be hard coded, should be an option on the website
                string emailToAddress = "andrew.myhre@gmail.com";
                string emailToName = mWebsite.Title;
                if (mWebsite.getOption("ContactUsToAddress") != null)
                    emailToAddress = mWebsite.getOptionValue("ContactUsToAddress");
                if (mWebsite.getOption("ContactUsToName") != null)
                    emailToName = mWebsite.getOptionValue("ContactUsToName");
                mail.To.Add(new MailAddress(emailToAddress, emailToName));

                //set the content
                mail.Subject = "Contact Us from " + mWebsite.URL;
                mail.Body = message;
                mail.IsBodyHtml = false;

                string loginfo = string.Format("Sending contact us email\r\nfrom={0}, {1},\r\nto={2}, {3}\r\nsubject={4}\r\nmessage={5}",
                    senderName, senderEmail, emailToName, emailToAddress, mail.Subject, mail.Body);
                log.InfoFormat(loginfo);
                Trace.Write(loginfo);

                //send the message
                SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
                smtp.EnableSsl = true;
                smtp.UseDefaultCredentials = true;
                smtp.Credentials = new NetworkCredential("andrew.myhre", "zlenoET1");
                smtp.Send(mail);
            } catch (Exception ex)
            {
                Trace.Warn("Email", "Message could not be sent", ex);
                log.Error("Email couldn't be sent", ex);
            }
            messageSent = true;
        }
    }
}
